INITSEG     equ 0x9000
SYSSEG      equ 0x1000
SETUPSEG    equ 0x9020

    global  begtext, begdata, begbss, endtext, enddata, endbss

begtext:
begdata:
begbss:

start:
    
    mov ax, INITSEG
    mov ds, ax
    mov ah, 0x03
    xor bh, bh
    int 0x10
    mov [0], dx

    mov ah, 0x88
    int 0x15
    mov [2], ax

    mov ah, 0x0f
    int 0x10
    mov [4], bx
    mov [6], ax

    mov ah, 0x12
    mov bl, 0x10
    int 0x10
    mov [8], ax
    mov [10], bx
    mov [12], cx

    mov ax, 0x0000
    mov ds, ax
    lds si, [4 * 0x41]
    mov ax, INITSEG
    mov es, ax
    mov di, 0x0080
    mov cx, 0x10
    rep
    movsb

    mov ax, 0x0000
    mov ds, ax
    lds si, [4 * 0x46]
    mov ax, INITSEG
    mov es, ax
    mov di, 0x0090
    mov cx, 0x10
    rep
    movsb

    mov ax, 0x1500
    mov dl, 0x81
    int 0x13
    jc  no_disk1
    cmp ah, 3
    je  is_disk1
no_disk1:
    mov ax, INITSEG
    mov es, ax
    mov di, 0x0090
    mov cx, 0x10
    mov ax, 0x00
    rep
    stosb
is_disk1:
    cli

    mov ax, 0x0000
    cld
do_move:
    mov es, ax
    add ax, 0x1000
    cmp ax, 0x9000
    jz  end_move
    mov ds, ax
    sub di, di
    sub si, si
    mov cx, 0x8000
    rep
    movsw
    jmp do_move

end_move:
    mov ax, SETUPSEG
    mov ds, ax
    lidt    [idt_48]
    lgdt    [gdt_48]

    call    empty_8042
    mov al, 0xd1
    out 0x64, al
    call    empty_8042
    mov al, 0xdf
    out 0x60, al
    call    empty_8042

    mov al, 0x11
    out 0x20, al
    dw  0x00eb, 0x00eb
    out 0xa0, al
    dw  0x00eb, 0x00eb
    mov al, 0x20
    out 0x21, al
    dw  0x00eb, 0x00eb
    mov al, 0x28
    out 0xa1, al
    dw  0x00eb, 0x00eb
    mov al, 0x04
    out 0x21, al
    dw  0x00eb, 0x00eb
    mov al, 0x02
    out 0xa1, al
    dw  0x00eb, 0x00eb
    mov al, 0x01
    out 0x21, al 
    dw  0x00eb, 0x00eb
    out 0xa1, al
    dw  0x00eb, 0x00eb
    mov al, 0xff
    out 0x21, al
    dw  0x00eb, 0x00eb
    out 0xa1, al

    mov ax, 0x0001
    lmsw    ax
    jmp 8:0

empty_8042:
    dw  0x00eb, 0x00eb
    in  al, 0x64
    test    al, 2
    jnz empty_8042
    ret

gdt:
    dw  0, 0, 0, 0
    dw  0x07ff, 0x0000, 0x9a00, 0x00c0
    dw  0x07ff, 0x0000, 0x9200, 0x00c0

idt_48:
    dw  0
    dw  0, 0

gdt_48:
    dw  0x800
    dw  512 + gdt, 0x9

endtext:
enddata:
endbss:
